package com.hq.es.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hq.es.pojo.User;
import com.hq.es.utils.ElasticsearchUtil;
import io.swagger.annotations.*;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.*;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.service.ParameterType;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

/**
* @author: lujie
* @create: 2021/4/11
* @description: Elasticsearch 索引相关操作
**/
@Api(tags = "索引相关操作")
@RestController
@RequestMapping("/index")
public class IndexController {


    @Resource
    private ElasticsearchUtil elasticsearchUtil;

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient restHighLevelClient;

    @PostMapping(path = "/{indexName}")
    @ApiOperation(value = "创建索引",notes = "DSL:PUT /indexName")
    @ApiImplicitParam(name = "indexName",value = "索引名称",required = true)
    public  Object create(@PathVariable String indexName) throws IOException {
        return    elasticsearchUtil.createIndex(indexName);
    }

    @PostMapping(path = "/createIndexAndMapping",produces = { APPLICATION_JSON_VALUE })
    @ApiOperation(value = "创建索引并指定索引映射",produces="application/json")
    @ApiImplicitParam(name = "mappings",value = "索引名称",required = true,paramType = "body")
    public  Object createIndexAndMapping( @RequestBody JSONObject jsonObject) throws Exception {
        String indexName = jsonObject.getStr("indexName");
         Map mapping = jsonObject.get("mappings", Map.class);
        CreateIndexRequest createIndexRequest =  new CreateIndexRequest(indexName);;
        createIndexRequest.mapping(mapping);
        IndicesClient indicesClient = restHighLevelClient.indices();
        //执行创建索引库
        CreateIndexResponse response = indicesClient.create(createIndexRequest, RequestOptions.DEFAULT);
        return response;
    }

    @GetMapping(path = "/catIndexMapping/{indexName}")
    @ApiOperation(value = "查看索引Mapping",notes = "GET /indexName/_mapping ")
    @ApiImplicitParam(paramType = "path",name = "indexName",value = "索引名称",required = true)
    public  Object catIndexMapping(@PathVariable String indexName) throws IOException {
        return       elasticsearchUtil.catIndexMappings(indexName);
    }


    @DeleteMapping(path = "/{indexName}")
    @ApiOperation(value = "删除索引",notes = "DELETE /indexName")
    @ApiImplicitParam(name = "indexName",value = "索引名称",required = true)
    public  Object delete(@PathVariable String indexName) throws IOException {
        return     elasticsearchUtil.deleteIndex(indexName);
    }

    @GetMapping(path = "exist/{indexName}")
    @ApiOperation(value = "判断索引是否存在")
    @ApiImplicitParam(name = "indexName",value = "索引名称",required = true)
    public  Object existIndex(@PathVariable String indexName) throws IOException {
        return     elasticsearchUtil.isIndexExist(indexName);
    }

    @GetMapping
    @ApiOperation(value = "查询所有索引",notes = "GET /_cat/indices?v")
    public  Object queryIndex( ) throws IOException {
        Object allIndex = elasticsearchUtil.findAllIndex();
        return   allIndex;
    }






}
